package cn.rui.controller;

import cn.hutool.http.HttpUtil;
import cn.rui.common.ResponseBean;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 徽州大都督
 * @date 2021/11/19
 */
@RestController
@RequestMapping("media")
@Api(tags = "媒体模块")
@Slf4j
public class MediaController {

    @GetMapping("videoDeal")
    public ResponseBean videoDeal(String url) {

        int https = url.lastIndexOf ("https");
        String substring = url.substring (https);

        int index = substring.lastIndexOf ("复制");

        String substring1 = substring.substring (0, index);

        String content = HttpUtil.get ("https://tenapi.cn/video/?url=" + substring1.trim ());

        JSONObject jsonObject = JSONObject.parseObject (content);
        String code = jsonObject.getString ("code");

        Map map = new HashMap ();
        if (code.equals ("200")) {
            log.info ("第一次请求成功:" + content);
            map.put ("title", jsonObject.getString ("title"));
            map.put ("cover", jsonObject.getString ("cover"));
            map.put ("url", jsonObject.getString ("url"));
            map.put ("music", jsonObject.getString ("music"));
            return ResponseBean.success ("解析成功", map);
        } else {
            String content2 = HttpUtil.get ("https://tenapi.cn/douyin/?url=" + url);
            JSONObject jsonObject2 = JSONObject.parseObject (content2);
            String code2 = jsonObject2.getString ("code");
            if (code2.equals ("200")) {
                log.info ("第二次请求成功:" + content2);
                map.put ("title", jsonObject2.getString ("title"));
                map.put ("cover", jsonObject2.getString ("cover"));
                map.put ("url", jsonObject2.getString ("url"));
                map.put ("music", jsonObject2.getString ("music"));
                return ResponseBean.success ("解析成功", map);
            } else {
                String content3 = HttpUtil.get ("https://tenapi.cn/kuaishou/?url=" + url);
                System.out.println (content3);
                JSONObject jsonObject3 = JSONObject.parseObject (content3);
                String code3 = jsonObject3.getString ("code");
                if (code3.equals ("200")) {
                    log.info ("第三次请求成功:" + content3);
                    map.put ("title", jsonObject3.getString ("title"));
                    map.put ("cover", jsonObject3.getString ("cover"));
                    map.put ("url", jsonObject3.getString ("url"));
                    map.put ("music", jsonObject3.getString ("music"));
                    return ResponseBean.success ("解析成功", map);
                }

                return ResponseBean.error ("解析失败");

            }
        }
        //return ResponseBean.error ("解析失败");
    }
}
